/**
 * 验证完整的视频文件里面音视频帧的读取
 * 特点如下：
 * 1. 音视频对齐比较粗暴，一帧视频对齐一帧音频，都是40ms，帧率25
 * 2. 音视频帧，每40ms返回一次
 */
const {VideoFrameReader} = require("./VideoHelper");
const {writeData} = require("../../util/util")

let param = {
    sessionId: 'xxyy1122',
    // videoFile: 'data/test.mp4',
    videoFile: 'D:\\media\\18s_fps25_200x300_48000_mono.mp4',
    videoFps: 25,
    videoWidth: 145,
    videoHeight: 200,
    audioSampleRate: 48000,
    audioChannelCount: 1,
    debug:{
        sum:false
    }
}

async function test_auto_emit_frame(){
    param = {...param, isAutoEmitFrame:true};
    let videoHelper = new VideoFrameReader(param);
    let lastTime = Date.now();
    await videoHelper.start();
    videoHelper.on('audio_video_frame', param=>{
        if (!param.isReadOver) {
            let currentTime = Date.now();
            console.log(`收到音视频帧, delta:${currentTime - lastTime}, param:${param}`);
            lastTime = currentTime;
        }else{
            console.log('!!!!!!!!!!!!!!!!!!文件读取结束!!!!!!!!!!!!!!!!!!');
        }
    })
}

async function test_get_frame_manually(){
    return new Promise(async (resolve, reject) =>{
        param = {...param, isAutoEmitFrame:false};
        let videoHelper = new VideoFrameReader(param);
        let lastTime = Date.now();
        await videoHelper.start();

        let myInterval = setInterval(()=>{
            let frame = videoHelper.getFrame();
            if (!frame) {
                console.log('当前没有读取到帧');
            }else{
                let {isFirstFrame, isReadOver, videoFrame} = frame;
                if (isReadOver) {
                    console.log('读取结束，退出读取流程');
                    clearInterval(myInterval);
                    resolve('读取结束');
                }

                //写一帧就退出，主要用于测试
                if (isFirstFrame) {
                    writeData([videoFrame], 'test11.rgba');
                    clearInterval(myInterval);
                    return;
                }

                let currentTime = Date.now();
                console.log(`收到音视频帧2, delta:${currentTime - lastTime}, param:${param}, isFirst:${isFirstFrame}, isReadOver:${isReadOver}`);
                lastTime = currentTime;
            }
            let currentTime = Date.now();
            console.log(`收到音视频帧2, delta:${currentTime - lastTime}`);
            lastTime = currentTime;
            console.log('test');
        }, 40);
    } )
}

/**
 * 连续读取50次
 * @returns {Promise<void>}
 */
async function test_get_frame_manually_50times(){
    for (let i = 0; i < 50; i++) {
        console.log(`当前测试次数为:${i}`);
        await test_get_frame_manually();
    }
}

test_get_frame_manually()

